package hfuu.com.cn.teasystemapp;

import android.app.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;

import java.io.IOException;
import java.io.Serializable;
import java.net.CookieHandler;
import java.net.CookieStore;
import java.net.HttpCookie;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import hfuu.com.cn.teasystemapp.bean.role;
import hfuu.com.cn.teasystemapp.util.HttpUtils;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

//import com.squareup.okhttp.OkHttpClient;

public class Login extends AppCompatActivity  {
    private SharedPreferences preference;
    private SharedPreferences.Editor editor;
    Map map;
    String jason="";
    private List<role> roleList=new ArrayList<role>();
    public String user;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        user="";
        Button button = (Button) findViewById(R.id.btn_login);
        preference=getSharedPreferences("cookie", MODE_PRIVATE);
        editor=preference.edit();
        map=new HashMap();
        final Response response;
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText usertext = (EditText) findViewById(R.id.username);
                EditText passtext = (EditText) findViewById(R.id.password);
                user = usertext.getText().toString().trim();
                String pass = passtext.getText().toString().trim();

                if (user.length() == 0 || pass.length() == 0) {
                    Toast.makeText(Login.this, "用户名或密码不能为空", Toast.LENGTH_LONG).show();
                } else {


                    map.put("username",user);
                    map.put("password",pass);


                    final Thread thread=new Thread(new Runnable() {
                        @Override
                        public void run() {
                            HttpUtils.doPost("login/dologinad", map, new Callback() {
                                public Response response;

                                @Override
                                public void onFailure(Call call, IOException e) {
                                    Looper.prepare(); Toast.makeText(Login.this,"网络错误",Toast.LENGTH_LONG).show();
                                    Looper.loop();

                                }

                                @Override
                                public void onResponse(Call call, Response response) throws IOException {

                                    jason=response.body().string();
                                    this.response=response;
                                    parseJSONWithGSON(jason);
                                    Headers headers=response.headers();
                                    List<String> cookies = headers.values("Set-Cookie");
                                    Message msg = new Message();
                                    msg.what = 1;
                                    uiHandler.sendMessage(msg);

                                }
                            });


                        }
                    });
                    thread.start();

                }
            }
        });
    }


    private void parseJSONWithGSON(String jsonData) {
        //使用轻量级的Gson解析得到的json
        Gson gson = new Gson();
       roleList = (ArrayList<role>)gson.fromJson(jsonData, new TypeToken<List<role>>() {}.getType());
    }


    private Handler uiHandler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    Log.i("useerror",jason);
                    if(jason==null||jason.equals("")){
                        Toast.makeText(Login.this,"用户名或密码错误",Toast.LENGTH_LONG).show();

                        //

                    }else{
                        Intent intent=new Intent();
                        intent.setClass(Login.this,MainActivity.class);
                        intent.putExtra("username",user);
                        intent.putExtra("role",(Serializable) roleList);
                        startActivity(intent);
                        finish();
                    }
                    break;
            }

        }
    };
}
